import 'package:common_utils/common_utils.dart';
import 'package:erp/data/model/sale_project_detail.dart';
import 'package:erp/res/colors.dart';
import 'package:erp/util/number_to_chinese.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'my_button.dart';

/// 项目详细情况 组件
class SaleProjectDetailList extends StatefulWidget {
  final List<SaleProjectDetail> saleProjectDetailList;
  final bool readOnly;
  final Future<bool> Function(int index)? onEdit;
  final Future<bool> Function()? onAdd;
  final Function(dynamic data)? onCheck;

  SaleProjectDetailList({
    Key? key,
    required this.saleProjectDetailList,
    this.readOnly = false,
    this.onEdit,
    this.onAdd,
    this.onCheck,
  });

  @override
  State<StatefulWidget> createState() {
    return _SaleProjectDetailListState(
      saleProjectDetailList: saleProjectDetailList,
      readOnly: readOnly,
      onEdit: onEdit,
      onAdd: onAdd,
      onCheck: onCheck,
    );
  }
}

class _SaleProjectDetailListState extends State<SaleProjectDetailList> {
  final List<SaleProjectDetail> saleProjectDetailList;
  final bool readOnly;
  final Future<bool> Function(int index)? onEdit;
  final Future<bool> Function()? onAdd;
  final Function(dynamic data)? onCheck;

  _SaleProjectDetailListState({
    required this.saleProjectDetailList,
    required this.readOnly,
    this.onEdit,
    this.onAdd,
    this.onCheck,
  });

  @override
  Widget build(BuildContext context) {
    double _totalAmount = 0.00;
    if (saleProjectDetailList.isNotEmpty) {
      _totalAmount = saleProjectDetailList.map((e) => e.detailAmount ?? 0).reduce(
            (value, element) => NumUtil.add(value, element),
          );
    }
    // 这里要做计算 0.0
    var cards = saleProjectDetailList.asMap().entries.map((entry) {
      int index = entry.key;
      SaleProjectDetail spd = entry.value;
      var buttons = !readOnly
          ? [
              Container(
                constraints: BoxConstraints(maxWidth: 60.w, maxHeight: 30.h),
                child: MyButton(
                  text: '编辑',
                  onPressed: () async {
                    if (onEdit != null) {
                      var flag = await onEdit!(index);
                      if (flag) {
                        setState(() {});
                      }
                    }
                  },
                  fontSize: 16.sp,
                  backgroundColor: Color(0XFF4C88FF),
                  padding: EdgeInsets.all(0),
                  radius: 4.r,
                ),
              ),
              Container(
                constraints: BoxConstraints(maxWidth: 60.w, maxHeight: 30.h),
                child: MyButton(
                  text: '删除',
                  onPressed: () {
                    setState(() {
                      saleProjectDetailList.removeAt(index);
                    });
                  },
                  fontSize: 16.sp,
                  backgroundColor: Colors.white,
                  textColor: Color(0XFF666666),
                  padding: EdgeInsets.all(0),
                  radius: 4.r,
                  side: BorderSide(color: Color(0XFF4C88FF), width: 1.w),
                ),
              )
            ]
          : [
              Container(
                constraints: BoxConstraints(maxWidth: 60.w, maxHeight: 30.h),
                child: MyButton(
                  text: '查看',
                  onPressed: () {
                    if (onCheck != null) {
                      onCheck!(spd);
                    }
                  },
                  fontSize: 16.sp,
                  backgroundColor: Color(0XFF4C88FF),
                  padding: EdgeInsets.all(0),
                  radius: 4.r,
                  side: BorderSide(color: Color(0XFF4C88FF), width: 1.w),
                ),
              )
            ];
      var buttonGroup = Container(
        width: (buttons.length * 60 + (buttons.length - 1) * 10).w,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [...buttons],
        ),
      );
      return SaleProjectDetailCard(
        buttonGroup: buttonGroup,
        price: spd.detailAmount,
        content: spd.detailContent,
      );
    });
    String totalNumFormat = '¥${TextUtil.formatDoubleComma3(_totalAmount)}';
    String totalNumChineseFormat = ConvertNumberToChineseMoneyWords().toChinese(_totalAmount.toString());

    var addWidget;
    if (!readOnly) {
      addWidget = Container(
        color: Colors.transparent,
        height: 70.h,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            Text(
              '添加列表',
              style: TextStyle(
                color: Color(0XFF666666),
                fontSize: 22.sp,
                fontWeight: FontWeight.w400,
              ),
            ),
            SizedBox(width: 10.w),
            Image.asset(
              'assets/images/right_arrow.png',
              width: 12.w,
              height: 20.h,
              color: Colors.black,
            )
          ],
        ),
      );
      addWidget = Material(
        child: Ink(
          color: Colors.white,
          child: InkWell(
            onTap: () async {
              if (onAdd != null) {
                var result = await onAdd!();
                if (result) {
                  setState(() {});
                }
              }
            },
            child: addWidget,
          ),
        ),
      );
    } else {
      addWidget = SizedBox.shrink();
    }

    return Column(
      children: [
        Container(
          constraints: BoxConstraints(minHeight: 142.h),
          padding: EdgeInsets.fromLTRB(20.w, 48.h, 20.w, 30.h),
          // height: 142.h,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    '合计合同金额：',
                    style: TextStyle(
                      color: Colours.FORM_FIELD_VALUE_COLOR,
                      fontSize: 24.sp,
                    ),
                  ),
                  Text(
                    totalNumFormat,
                    style: TextStyle(
                      color: Color(0XFFFF9500),
                      fontSize: 24.sp,
                    ),
                  )
                ],
              ),
              SizedBox(height: 17.h),
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  Text(
                    totalNumChineseFormat,
                    style: TextStyle(
                      color: Color(0XFF999999),
                      fontSize: 20.sp,
                      fontWeight: FontWeight.w400,
                    ),
                  )
                ],
              ),
            ],
          ),
        ),
        Divider(
          height: 1.h,
          color: Colours.TEXT_GRAY_C,
          thickness: 1.w,
        ),
        SizedBox(height: 10.h),
        ...cards,
        SizedBox(height: 10.h),
        addWidget
      ],
    );
  }
}

/// 卡片
class SaleProjectDetailCard extends StatelessWidget {
  final double? price;
  final String? content;
  final Widget? buttonGroup;

  SaleProjectDetailCard({
    Key? key,
    this.price = 0.00,
    this.content = '',
    this.buttonGroup,
    // required this.saleProjectDetail,
  });

  get priceFormat => '¥' + TextUtil.formatDoubleComma3(price ?? 0.00);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(top: 20.h),
      padding: EdgeInsets.fromLTRB(20.w, 30.h, 20.w, 25.h),
      constraints: BoxConstraints(minWidth: 686.w, minHeight: 207.h),
      decoration: BoxDecoration(
        color: Colors.white,
        boxShadow: [
          BoxShadow(
            offset: Offset(3.w, 5.w),
            blurRadius: 15.w,
            spreadRadius: 0,
            color: Color.fromRGBO(38, 68, 128, 0.12),
          ),
        ],
        borderRadius: BorderRadius.all(
          Radius.circular(10.r),
        ),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(content!, style: TextStyle(color: Color(0XFF666666), fontSize: 24.sp, fontWeight: FontWeight.w400)),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              RichText(
                text: TextSpan(
                  text: '预估合同金额：',
                  style: TextStyle(color: Color(0XFF666666), fontSize: 24.sp, fontWeight: FontWeight.w400),
                  children: [
                    TextSpan(
                      text: priceFormat,
                      style: TextStyle(color: Color(0XFFFF9500), fontSize: 24.sp, fontWeight: FontWeight.w400),
                    ),
                  ],
                ),
              ),
              buttonGroup ?? SizedBox.shrink(),
            ],
          )
        ],
      ),
    );
  }
}
